package com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetFixedStorage;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetFixedStorageMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.SmsAssetMyAssets;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.dto.SmsAssetMyAssetsDTO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.mapper.SmsAssetMyAssetAndStorageMpjMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.mapper.SmsAssetMyAssetsMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.service.SmsAssetMyAssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 我的资产 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-06-04
 */
@Service
public class SmsAssetMyAssetsServiceImpl extends ServiceImpl<SmsAssetMyAssetsMapper, SmsAssetMyAssets> implements SmsAssetMyAssetsService {
    @Resource
    private SmsAssetMyAssetsMapper smsAssetMyAssetsMapper;
    @Resource
    private SmsAssetFixedStorageMapper smsAssetFixedStorageMapper;
    @Resource
    private SmsAssetCategoryManagementMapper smsAssetCategoryManagementMapper;

    @Autowired
    private SmsAssetMyAssetAndStorageMpjMapper smsAssetMyAssetAndStorageMpjMapper;

    @Override
    public ResponseResult getMyAssetList( String asset_name, String asset_model,
                                          String asset_brand, String asset_category_id,
                                          Integer my_asset_status, BigDecimal asset_price,
                                          String asset_place,Integer pageSize, Integer pageNum, String user_id) {
        if(isNullOrEmpty(user_id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<SmsAssetMyAssetsDTO> list;

        MPJLambdaWrapper<SmsAssetMyAssets> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(SmsAssetMyAssets.class)
                .selectAll(SmsAssetFixedStorage.class)
                .selectAs(SmsAssetMyAssets::getId,"myAssetId")
                .selectAs(SmsAssetFixedStorage::getAssetCategory,"assetCategory")
                .selectAs(SmsAssetFixedStorage::getAssetBrand,"assetBrand")
                .selectAs(SmsAssetFixedStorage::getAssetModel,"assetModel")
                .selectAs(SmsAssetFixedStorage::getAssetUnit,"assetUnit")
                .selectAs(SmsAssetFixedStorage::getAssetPlace,"assetPlace")
                .selectAs(SmsAssetFixedStorage::getAssetPrice,"assetPrice")
                .selectAs(SmsAssetFixedStorage::getAssetName,"assetName")
                .leftJoin(SmsAssetFixedStorage.class,SmsAssetFixedStorage::getId,SmsAssetMyAssets::getAssetId)
                .eq(SmsAssetMyAssets::getUserId,user_id);

        if (!isNullOrEmpty(my_asset_status)){
            queryWrapper.eq(SmsAssetMyAssets::getMyAssetStatus,my_asset_status);
        }
        if (!isNullOrEmpty(asset_name)){
            queryWrapper.like(SmsAssetFixedStorage::getAssetName,asset_name);
        }
        if (!isNullOrEmpty(asset_model)){
            queryWrapper.like(SmsAssetFixedStorage::getAssetModel,asset_model);
        }
        if (!isNullOrEmpty(asset_brand)){
            queryWrapper.like(SmsAssetFixedStorage::getAssetBrand,asset_brand);
        }
        if (!isNullOrEmpty(asset_price)){
            queryWrapper.eq(SmsAssetFixedStorage::getAssetPrice,asset_price);
        }
        if (!isNullOrEmpty(asset_place)){
            queryWrapper.like(SmsAssetFixedStorage::getAssetPlace,asset_place);
        }
        if (!isNullOrEmpty(asset_category_id)){
            queryWrapper.eq(SmsAssetFixedStorage::getAssetCategory,asset_category_id);
        }

        Page<SmsAssetMyAssetsDTO> page = new Page<>(pageNum,pageSize);
        smsAssetMyAssetAndStorageMpjMapper.selectJoinPage(page,SmsAssetMyAssetsDTO.class,queryWrapper);

        list = page.getRecords()
                .stream()
                .map(storage -> {
                    SmsAssetCategoryManagement categoryManagement = smsAssetCategoryManagementMapper.selectById(storage.getAssetCategory());
                    if (categoryManagement != null ){
                        storage.setAssetCategoryName(categoryManagement.getCategoryName());
                    }
                    return storage;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }
}
